#include "motor.h"

int32 bldc_b_speed = 0;
int32 bldc_f_speed = 0;
float bdc_speed = 0;//有刷期望速度
float bdc_speed1 = 345;//有刷期望速度
float bdc_speed_max = 0;
int16 zuopwm=0,youpwm=0;

//有刷初始化
void motor_int(void){


    pwm_init(BDC_PWM_PIN, 17000, 0);           // 定义有刷电机PWM引脚
    gpio_init(P21_2, GPO, 1, GPO_PUSH_PULL);//初始化方向引脚

    encoder_quad_init(TIM4_ENCODER, TIM4_ENCODER_CH1_P02_8, TIM4_ENCODER_CH2_P00_9);//有刷编码器初始化

}

//设置有刷PWM    死区
void SET_BDC_PWM(int ERROR) {

    if (ERROR >= 8000)ERROR = 8000;
    else if (ERROR <= -8000)ERROR = -8000;

    if (ERROR >= 0) {
        gpio_high(P21_2);
        ERROR=ERROR+880;
        pwm_set_duty(BDC_PWM_PIN, ERROR);

    }
    else {
        gpio_low(P21_2);
        ERROR=ERROR-900;
        pwm_set_duty(BDC_PWM_PIN, -ERROR);

    }
}
//设置左右无刷速度   PWM_DUTY_MAX（10000）
void SET_wushua_PWM(int16 zuoERROR,int16 youERROR) {

    if (zuoERROR > PWM_DUTY_MAX){zuoERROR = PWM_DUTY_MAX;}
    else if (zuoERROR < -PWM_DUTY_MAX){zuoERROR = -PWM_DUTY_MAX;}

    if (youERROR > PWM_DUTY_MAX){youERROR = PWM_DUTY_MAX;}
    else if (youERROR < -PWM_DUTY_MAX){youERROR = -PWM_DUTY_MAX;}
    zuopwm= zuoERROR;
    youpwm= youERROR;

    small_driver_set_duty(zuopwm,youpwm);//设置左右无刷速度

}





